package com.example.controller;

import com.example.bean.crm.ckb;
import com.example.service.crmService.crmService;
import com.example.service.k3Service.k3Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 销售出库模块
 * Created by tony on 2017/12/15.
 */
@Component
@RestController
public class ckControl {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    //定义k3Service
    @Autowired
    private k3Service k3Service;
    //定义mesService
    @Autowired
    private crmService crmService;

    /**
     * 销售出库更新
     */
    @RequestMapping("/insertCk")
    @Scheduled(cron = "0 0/5 * * * ?") //每5执行一次
    public void insertCkScheduled() {
        logger.info("销售出库更新。开始……");
        try {
            //取得更新的销售出库
            ArrayList<ckb> cklist = k3Service.selectCk();
            for (ckb ckb : cklist) {
                //中间表中插入销售出库
                crmService.insertCk(ckb);
                //标示销售出库已经插入中间表
                k3Service.updateCk(ckb.getCkdID());
            }

        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        logger.info("销售出库更新。结束！");
    }

    /**
     * 销售出库经销商信息
     */
    @RequestMapping("/returnJxs")
    @Scheduled(cron = "0 0/5 * * * ?") //每5执行一次
    public void insertReturnJxscheduled() {
        logger.info("经销商收货反写。开始……");
        try {
            //取得更新的销售出库
            ArrayList<ckb> jxsshlist = crmService.selectCk();
            for (ckb jxssh : jxsshlist) {
                //经销商收货反写
                k3Service.updateJxssh(jxssh);
                //中间表中处理反写
                crmService.updateCk(jxssh);
            }

        } catch (Exception e) {
            logger.info(e.getMessage());
        }
        logger.info("经销商收货反写。结束！");
    }
}
